package com.qq.Servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 这里的注解就是省略了web.xml里面的配置
 */
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {


    @Override
    public void init() throws ServletException {
        System.out.println("init()被创建了");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out =resp.getWriter();
        out.write("service()进行处理");
        System.out.println("service被调用");
        //重定向
        resp.sendRedirect("index.jsp");
// 这里都是实现转发
     //   String appName = getServletContext().getInitParameter("appName");
      //  String appName1 = req.getServletContext().getInitParameter("appName");
      //  System.out.println("得到的应用程序名为"+appName+"1111"+appName1);
        //在前端页面输出出来
    //    out.write("得到的应用程序名为"+appName+"1111"+appName1);

        //第二种方式进行转发
       // req.setAttribute("appName",appName);
       // req.getRequestDispatcher("V.jsp").forward(req,resp);

//这是第一种实现转发
//        String name = this.getInitParameter("name");
//        String name1 = this.getInitParameter("name");
//        out.write("hysggugyugufuguygfu"+name+"%"+name1);

        HttpSession session = req.getSession();
        session.invalidate();

    }

    //这里可以通过Code里面的覆盖父类方法来生成
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out =resp.getWriter();
        out.write("post方式提交");
        System.out.println("测试该方法是否被调用");
    }

    public HelloServlet(){
        System.out.println("构造方法");
    }
//这里可以通过Code里面的覆盖父类方法来生成
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决乱码问题
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("doGet()被调用");
        //resp.setCharacterEncoding("utf-8");
        PrintWriter out =resp.getWriter();
        out.write("hello,小明");
        //405就是它输出出来的
       // super.doGet(req, resp);
    }

    @Override
    public void destroy() {
        System.out.println("destroy()进行销毁");
    }
}
